<template>
  <el-dialog :visible.sync="visible" :close-on-click-modal="false" width="600px" :show-close="false" class="contract-from" @close="closeVisble">
    <span slot="title" class="dialog-title">
      <div class="dialog-title-left">
        {{ title }}
      </div>
      <div class="dialog-title-right">
        <el-button @click="closeVisble">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm('ruleForm')">提 交</el-button>
      </div>
    </span>
    <el-form ref="ruleForm" label-position="top" :model="ruleForm" :rules="rules" label-width="auto" class="demo-ruleForm">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="员工">
            <el-select
              v-model="ruleForm.empId"
              filterable
              :disabled="id !== 0"
              remote
              reserve-keyword
              placeholder="请选择员工"
              :remote-method="remoteMethod"
              style="width: 100%"
            >
              <el-option
                v-for="item in options"
                :key="item.ID"
                :label="item.Name"
                :value="item.ID"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="角色配置">
            <el-checkbox-group v-model="checkList">
              <el-checkbox v-for="item in roleTypeArr" :key="item.ID" :label="item.ID"> {{ item.name }} </el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-dialog>
</template>

<script>
import { rulesRole } from './config'
import { getEmpSearch } from '@/api/employee'
import { getRoleList, addRoleEmp } from '@/api/role'
export default {
  name: 'AddRoleEmpFrom',
  props: {
    title: {
      type: String,
      default: '员工管理'
    },
    visible: {
      type: Boolean,
      default: false
    },
    name: {
      type: String,
      default: ''
    },
    id: {
      type: Number,
      default: 0
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      ruleForm: {
        empId: ''
      },
      options: [],
      checkList: [],
      roleTypeArr: [],
      rules: rulesRole
    }
  },
  watch: {
    visible: {
      handler(value) {
        if (value) {
          this.init()
        }
        if (value && this.id) {
          this.checkList = this.list
          this.ruleForm.empId = this.id
          this.options.push({ ID: this.id, Name: this.name })
        }
      }
    }
  },
  methods: {
    async init() {
      const res = await getRoleList()
      if (res.ret === 0) {
        this.roleTypeArr = res.data
      }
    },
    async remoteMethod(query) {
      if (query !== '') {
        this.loading = true
        const res = await getEmpSearch(query)
        this.options = res.data
        setTimeout(() => {
          this.loading = false
        }, 200)
      } else {
        this.options = []
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.addRole()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    async addRole() {
      const res = await addRoleEmp({ id: this.ruleForm.empId, role_id: this.checkList.join(',') })
      if (res.ret === 0) {
        this.$message.success('操作成功')
        this.$emit('success')
      }
    },
    closeVisble() {
      if (this.$refs['ruleForm']) {
        this.$refs['ruleForm'].resetFields()
      }
      this.checkList = []
      this.options = []
      this.ruleForm = {
        empId: ''
      }
      this.$emit('close')
    }
  }
}
</script>

<style lang="scss">
.contract-from {
  .el-dialog__header {
    padding: 0;
  }
  .el-input-number {
    text-align: left!important;
  }
}
</style>
